package com.example.luozizhao.eprxdemo.di.module;

import com.example.luozizhao.eprxdemo.commom.ServerUrl;
import com.example.luozizhao.eprxdemo.http.api.ApiService;
import com.example.luozizhao.eprxdemo.http.cookie.AddCookiesInterceptor;
import com.example.luozizhao.eprxdemo.http.cookie.ReceivedCookiesInterceptor;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by luozizhao on 2018/5/16.
 */
@Module
public class HttpModle {

    @Singleton
    @Provides
    public ApiService providesApiService(Retrofit retrofit) {
        return retrofit.create(ApiService.class);
    }


    @Singleton
    @Provides
    public Retrofit providesRetrofit(Retrofit.Builder builder, OkHttpClient client) {
        return builder.client(client).build();
    }


    @Singleton
    @Provides
    public Retrofit.Builder providesRetroBuilder() {
        return new Retrofit.Builder().baseUrl(ServerUrl.SERVER_ADDRESS)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());
    }

    @Singleton
    @Provides
    public OkHttpClient providesOkhttpClient() {
        return new OkHttpClient().newBuilder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
                .addInterceptor(new AddCookiesInterceptor())
                .addInterceptor(new ReceivedCookiesInterceptor())
                //可以增加连接时间什么的
                .build();
    }

}
